<?php
class AdvertisementsController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		$this->forward('advertisements/advertisers/');
	}
	
	public function actionAdvertisers(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list menu
		$args = array();
		$arrClients = Advertisements::getInstance()->getListClients($intPage, $intRowsPerPage);
		$intTotalRows = isset($arrClients[0]['total_rows'])?$arrClients[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('advertisers', array(
			'arrClients' => $arrClients,
			'objPages' => $objPages,
			'args' => $args
		));	
	}
	
	public function actionUpdateAdvertiser(){
		$arrItemInfo = array();
		$intId = Yii::app()->request->getParam('id', null);
		//if post data
		if(Yii::app()->request->isPostRequest)
		{
			$intResult = 0;
			$arrItemInfo = Yii::app()->request->getParam('client', null);
			if(empty($intId)){
				//insert
				$intResult = Advertisements::getInstance()->insertClient($arrItemInfo);
			}
			else{
				//update
				$intResult = Advertisements::getInstance()->updateClient($intId, $arrItemInfo);
			}
			
			if($intResult > 0){
				Yii::app()->admin->setFlash('ok', "updated successfully!");
				$this->refresh();	
			}
		}
		//load form
		if(!empty($intId)){
			$arrItemInfo = Advertisements::getInstance()->getClient($intId);
		}
		//render layout
		$this->render('update-advertiser', array(
			'intId' => $intId,
			'arrItemInfo' => $arrItemInfo
		));
	}

	public function actionDeleteAdvertiser(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Advertisements::getInstance()->deleteClient($intId);
		header('Location: '.LINK_ADMINCP_ADVERTISEMENTS.'/advertisers');
	}

	public function actionZones(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list zones
		$args = array();
		$arrZones = Advertisements::getInstance()->getListZones($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrZones[0]['total_rows'])?$arrZones[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('zones', array(
			'arrZones' => $arrZones,
			'objPages' => $objPages,
			'args' => $args
		));	
	}
	
	public function actionUpdateZone(){
		$arrItemInfo = array();
		$intId = Yii::app()->request->getParam('id', null);
		//if post data
		if(Yii::app()->request->isPostRequest)
		{
			$intResult = 0;
			$arrItemInfo = Yii::app()->request->getParam('zone', null);
			if(empty($intId)){
				//insert
				$intResult = Advertisements::getInstance()->insertZone($arrItemInfo);
			}
			else{
				//update
				$intResult = Advertisements::getInstance()->updateZone($intId, $arrItemInfo);
			}
			
			if($intResult > 0){
				Yii::app()->admin->setFlash('ok', "updated successfully!");
				$this->refresh();	
			}
		}
		//load form
		if(!empty($intId)){
			$arrItemInfo = Advertisements::getInstance()->getZone($intId);
		}
		//render layout
		$this->render('update-zone', array(
			'intId' => $intId,
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionDeleteZone(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Advertisements::getInstance()->deleteZone($intId);
		header('Location: '.LINK_ADMINCP_ADVERTISEMENTS.'/zones');
	}

	public function actionCampaigns(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort', -1);
		$intClient = Yii::app()->request->getParam('client', null);
		//list campaigns
		$args = array(
			'client_id' => Yii::app()->request->getParam('client', $intClient)
		);
		$arrCampaigns = Advertisements::getInstance()->getListCampaigns($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrCampaigns[0]['total_rows'])?$arrCampaigns[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('campaigns', array(
			'arrCampaigns' => $arrCampaigns,
			'intClient' => $intClient,
			'objPages' => $objPages,
			'args' => $args
		));	
	}
	
	public function actionUpdateCampaign(){
		$arrItemInfo = array();
		$intId = Yii::app()->request->getParam('id', null);
		$intClient = Yii::app()->request->getParam('client', null);
		//if post data
		if(Yii::app()->request->isPostRequest)
		{
			$intResult = 0;
			$arrItemInfo = Yii::app()->request->getParam('campaign', null);
			$arrItemInfo['campaign_published_date'] = !empty($arrItemInfo['campaign_published_date'])?@date('Y-m-d H:i:s', strtotime($arrItemInfo['campaign_published_date'])):null;
			$arrItemInfo['campaign_expiry_date'] = !empty($arrItemInfo['campaign_expiry_date'])?@date('Y-m-d H:i:s', strtotime($arrItemInfo['campaign_expiry_date'])):null;

			if(empty($intId)){
				//insert
				$intResult = Advertisements::getInstance()->insertCampaign($arrItemInfo);
			}
			else{
				//update
				$intResult = Advertisements::getInstance()->updateCampaign($intId, $arrItemInfo);
			}
			
			if($intResult > 0){
				Yii::app()->admin->setFlash('ok', "updated successfully!");
				$this->refresh();	
			}
		}
		//load form
		if(!empty($intId)){
			$arrItemInfo = Advertisements::getInstance()->getCampaign($intId);
		}
		//render layout
		$this->render('update-campaign', array(
			'intId' => $intId,
			'intClient' => $intClient,
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionDeleteCampaign(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Advertisements::getInstance()->deleteCampaign($intId);
		header('Location: '.LINK_ADMINCP_ADVERTISEMENTS.'/campaigns');
	}
	
	public function actionBanners(){
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		$intCampaignId = Yii::app()->request->getParam('campaign', null);

		//list banners
		$arrBanners = Advertisements::getInstance()->getListBanners(array('campaign_id'=>$intCampaignId));	
		$intTotalRows = isset($arrBanners[0]['total_rows'])?$arrBanners[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render layout
		$this->render('banners', array(
			'intCampaignId' => $intCampaignId,
			'arrBanners' => $arrBanners,
			'objPages' => $objPages
		));
	}
	
	public function actionUpdateBanner(){
		$arrItemInfo = array();
		$intId = Yii::app()->request->getParam('id', null);
		$intCampaignId = Yii::app()->request->getParam('campaign', null);
		if(!empty($intCampaignId)){
			
			//if post data
			if(Yii::app()->request->isPostRequest){
				$arrItemInfo = Yii::app()->request->getParam('banner', null);
				if(empty($intId)){
					$intResult = Advertisements::getInstance()->insertBanner($arrItemInfo);
					$intId = $intResult;
				}
				else{
					$intResult = Advertisements::getInstance()->updateBanner($intId, $arrItemInfo); 
				}
				
				if($intResult > 0){
					//update zones
					$arrZones = Yii::app()->request->getParam('zones', null);
					if(!empty($arrZones)){
						//remove all banners
						Advertisements::getInstance()->deleteBannerZone($intId, null);
						foreach($arrZones as $R){
							//add new banners
							Advertisements::getInstance()->insertBannerZone($intId, $R);
						}
					}
				}
			}
			//load form
			if(!empty($intId)){
				$arrItemInfo = Advertisements::getInstance()->getBanner($intId);
			}
			$arrZones = Advertisements::getInstance()->getListZones(array(), 1, 100);
			//render layout
			$this->render('update-banner', array(
				'intCampaignId' => $intCampaignId,
				'arrItemInfo' => $arrItemInfo,
				'arrZones' => $arrZones
			));
		}
	}
	
	public function actionDeleteBanner(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Advertisements::getInstance()->deleteBanner($intId);
		header('Location: '.LINK_ADMINCP_ADVERTISEMENTS);
	}
}